#include <sw/redis++/redis.h>
#include <sw/redis++/queued_redis.h>
#include <iostream>
#include <chrono>
#include <unordered_set>

int main()
{   
    try
    {
        sw::redis::ConnectionOptions conn_opts = {
            .host = "192.168.194.128",
            .password = "123456"
        };
        sw::redis::ConnectionPoolOptions  pool_opts = {
            .size = 10,
            .connection_idle_time = std::chrono::milliseconds(3600)
        };
        sw::redis::Redis redis(conn_opts, pool_opts);

        {
            //pipeline 对象是 Redis 的管道机制，用于批量执行多个命令，减少网络往返开销。
            //false 参数表示不保证原子性（命令可能部分成功, 可能部分失败)
            auto pipe = redis.pipeline(false);
            pipe.set("key1", "value1");
            pipe.set("key2", "value2");
            pipe.set("key3", "value3");
            pipe.set("key4", "value4");
            pipe.get("key4");
            pipe.get("key3");
            auto reply = pipe.exec();
            std::cout << "结果数量: " << reply.size() <<std::endl;

            std::cout << reply.get<bool>(0) << std::endl;
            std::cout << reply.get<bool>(1) << std::endl;
            std::cout << reply.get<bool>(2) << std::endl;
            std::cout << reply.get<bool>(3) << std::endl;
            auto val4 = reply.get<sw::redis::OptionalString>(4);
            std::cout << "key4: " << *val4 << std::endl;
            auto val3 = reply.get<sw::redis::OptionalString>(5);
            std::cout << "key3: " << *val3 << std::endl;
            
            // 执行管道中的所有命令
            pipe.exec();
        }
    }
    catch(const sw::redis::Error& e)
    {
        std::cout << "redis error:" << e.what() << std::endl;
    }


    return 0;
}

